package org.dromara.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import okhttp3.MediaType;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.core.DefaultExcelListener;
import org.dromara.common.excel.core.ExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.WbCompanyInfo;
import org.dromara.system.domain.WbMinuteBidBag;
import org.dromara.system.domain.bo.WbCompanyBidBagBO;
import org.dromara.system.domain.bo.WbCompanyInfoBo;
import org.dromara.system.domain.bo.WbItemInfoBo;
import org.dromara.system.domain.dto.WbCompanyBidBagDTO;
import org.dromara.system.domain.dto.WbPlantEquipmentDTO;
import org.dromara.system.domain.vo.WbCompanyBlacklistVo;
import org.dromara.system.domain.vo.WbCompanyInfoVo;
import org.dromara.system.domain.vo.WbItemInfoVo;
import org.dromara.system.service.DianWangService;
import org.dromara.system.service.IWbCompanyInfoService;
import org.dromara.system.service.IWbProductPatentService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 公司
 *
 * @author Lion Li
 * @date 2024-03-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/companyInfo")
public class WbCompanyInfoController extends BaseController {

    private final IWbCompanyInfoService wbCompanyInfoService;

    private final IWbProductPatentService wbProductPatentService;

    private final DianWangService dianWangService;

    /**
     * 查询公司列表
     */
    @SaCheckPermission("system:companyInfo:list")
    @GetMapping("/list")
    public TableDataInfo<WbCompanyInfoVo> list(WbCompanyInfoBo bo, PageQuery pageQuery) {
        return wbCompanyInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询公司列表
     */
    @SaCheckPermission("system:companyInfo:list")
    @GetMapping("/listZ")
    public TableDataInfo<WbCompanyInfoVo> listZ(WbCompanyInfoBo bo, PageQuery pageQuery) {
        return wbCompanyInfoService.queryPageListZ(bo, pageQuery);
    }

    /**
     * 导出公司列表
     */
    @SaCheckPermission("system:companyInfo:export")
    @Log(title = "公司", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WbCompanyInfoBo bo, HttpServletResponse response) {
        List<WbCompanyInfoVo> list = wbCompanyInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "公司", WbCompanyInfoVo.class, response);
    }


    /**
     * 导入数据
     * @param file
     * @return
     * @throws Exception
     */
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @SaCheckPermission("system:companyInfo:import")
    @PostMapping(value = "/importData")
    public R<Void> importData(@RequestPart("file") MultipartFile file) {
        if (ObjectUtil.isNull(file)) {
            return R.fail("上传文件不能为空");
        }
        if(file.getOriginalFilename().indexOf(".zip") > 0){
            dianWangService.unzipCompanyInfo(file);
            return R.ok();
        }
        return R.fail("请上传.zip的压缩文件");
    }

    @SaCheckPermission("system:companyInfo:upload")
    @Log(title = "公司", businessType = BusinessType.IMPORT)
    @PostMapping("/upload")
    public R upload(WbCompanyInfoBo bo , @RequestParam("file") MultipartFile[] files) throws IOException {
        return wbCompanyInfoService.upload(bo, files);

    }

    /**
     * 获取公司详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<WbCompanyInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wbCompanyInfoService.queryById(id));
    }

    /**
     * 新增公司
     */
    @SaCheckPermission("system:companyInfo:add")
    @Log(title = "公司", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WbCompanyInfoBo bo) {
        bo.setStatus("-1");//设置默认状态：草稿
        return toAjax(wbCompanyInfoService.insertByBo(bo));
    }

    /**
     * 修改公司
     */
    @SaCheckPermission("system:companyInfo:edit")
    @Log(title = "公司", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WbCompanyInfoBo bo) {
        return toAjax(wbCompanyInfoService.updateByBo(bo));
    }

    /**
     * 删除公司
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:companyInfo:remove")
    @Log(title = "公司", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wbCompanyInfoService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 批量发布项目
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:companyInfo:edit")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PutMapping("/release/{ids}")
    public R<Void> release(@NotEmpty(message = "主键不能为空")
                           @PathVariable Long[] ids) {
        for (Long id : ids) {
            WbCompanyInfoVo wbCompanyInfoVo = wbCompanyInfoService.queryById(id);
            if("-1".equals(wbCompanyInfoVo.getStatus())){
                wbCompanyInfoVo.setStatus("0");
                wbCompanyInfoService.updateByBo(BeanUtil.toBean(wbCompanyInfoVo, WbCompanyInfoBo.class));
            }
        }
        return R.ok();
    }

}
